/**
 * Created by Johnny on 2017/1/29.
 */
var TenantRegister = function () {

    $.validator.addMethod('mobile', function( value, element ) {
        return this.optional( element ) || /^1[34578]\d{9}$/.test( value );
    }, "请输入有效的手机号码");

    $.validator.addMethod('phone', function( value, element ) {
        return this.optional( element ) || /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test( value );
    }, "请输入有效的座机号码");

    var handleSelect2 = function () {
        $('input[name="subscribe"]').select2({
            tags: ["生产制造", "财务", "行政审批", "金融", "保险", "医疗", "信贷", "矿业"]
        });
    }

    var handleBootstrapSelect = function(appl) {
        var val = 0;
        if (appl=='pro') {
            val = 1;
        } else if (appl=='ent') {
            val = 2;
        }
        $('.bs-select').selectpicker({
            iconBase: 'fa',
            tickIcon: 'fa-check'
        }).selectpicker('val', val);
    }

    var onError = function() {
        $('#succModal .modal-header').css("backgroundColor","red");
        $('#succModal h4').html("提交数据失败");
        $('#succModal .modal-body').html("我们抱歉的通知您！目前服务不可用，请稍后重试！");
        $('#succModal').modal({keyboard:true});
        setTimeout(function(){$("#succModal").modal("hide")},5000);
    }

    var reset = function() {
        document.getElementById("registerForm").reset();
        $(".select2-choices .select2-search-choice").remove();
        $('input[name="subscribe"]').val('');
    }

    var handleSubmit = function() {
        $("#btn_submit").click(function() {
            if ($("#registerForm").validate().form()) {
                $("#registerForm").ajaxSubmit({
                    type: 'post',
                    url: '/bpmp-engine/openapi/v1/tenant/register',
                    data: {

                    },
                    success: function (data) {
                        if (data && data.result) {
                            $('#succModal').modal({keyboard:false,backdrop:"static"});
                            reset(); // 提交后重置表单
                        } else {
                            onError();
                        }
                    },
                    error : onError
                });
            }
        });

        $("#btn_close").click(function(){
            closeWin()
        });

        $('#registerForm').validate({
            errorElement: 'span',
            errorClass: 'help-block',
            focusInvalid: true,
            onkeyup: false,
            ignore: "",
            rules: {

                name: {
                    required: true,
                    remote : {
                        url: "/bpmp-engine/openapi/v1/tenant/checkAttr",     //后台处理程序
                        type: "GET"
                    },
                    maxlength: 20
                },
                email: {
                    required: true,
                    email: true,
                    remote : {
                        url: "/bpmp-engine/openapi/v1/tenant/checkAttr",     //后台处理程序
                        type: "GET"
                    },
                    maxlength: 40
                },
                phone: {
                    required: true,
                    phone: true
                },
                mobile: {
                    required: true,
                    mobile: true
                },
                contact: {
                    required: true,
                    maxlength: 20
                },
                registeredAt: {
                    required: true,
                    maxlength: 40
                },
                street: {
                    required: true,
                    maxlength: 40
                },
                city: {
                    required: true,
                    maxlength: 20
                },
                province: {
                    required: true,
                    maxlength: 20
                },
                postCode: {
                    required: false,
                    maxlength: 6
                },
                tnc: {
                    required: true
                }
            },

            messages: {
                name : {
                    remote : "名称已被占用"
                },
                email : {
                    remote : "邮箱地址已被占用"
                },
                tnc: {
                    required: "请先接受条款"
                }
            },

            /*invalidHandler: function (event, validator) { //display error alert on form submit

            },*/

            highlight: function (element) { // hightlight error inputs
                $(element).closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                if (element.attr("name") == "tnc") { // insert checkbox errors after the container
                    error.insertAfter($('#register_tnc_error'));
                } else if (element.closest('.input-group').size() === 1) {
                    error.insertAfter(element.closest('.input-group'));
                } else {
                    error.insertAfter(element);
                }
            }/*,

            submitHandler: function (form) {
                form.submit();
            }*/
        });
    }

    var handleReset = function() {
        $("#btn_reset").click(function() {
            reset();
        });
    }

    return {
        //main function to initiate the module
        init: function (appl) {
            handleSelect2();
            handleBootstrapSelect(appl);
            handleSubmit();
            handleReset();
        }
    };

}();